package base.test2.serialize;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowReducer extends Reducer<Text,FlowBean,Text,FlowBean> {
    private FlowBean outV = new FlowBean();

    @Override
    protected void reduce(Text key,Iterable<FlowBean> values,Context context) throws IOException, InterruptedException {
        long totalUp = 0;
        long totalDown = 0;

        //遍历values，将其中的上行流量和下行流量分别进行累加
        for (FlowBean flowBean : values){
            totalUp += flowBean.getUpFlow();
            totalDown += flowBean.getDownFlow();
        }

        //封装outV，输出的key不用变还是手机号
        outV.setUpFlow(totalUp);
        outV.setDownFlow(totalDown);
        outV.setSumFlow(totalDown+totalUp);

        //输出
        context.write(key,outV);
    }

}
